package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.EstateCompanyDTO;
import org.springblade.modules.admin.entity.CompanyBuilding;
import org.springblade.modules.admin.entity.ElevatorType;
import org.springblade.modules.admin.entity.EstateCompany;

import org.springblade.modules.admin.service.BuildingService;
import org.springblade.modules.admin.service.CompanyBuildingService;
import org.springblade.modules.admin.service.EstateCompanyService;

import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2020/12/28 11:27 上午
 * @Author: Jackey
 * @Description: 物业公司表
 */
@RestController
@RequestMapping("/admin/estate_company")
public class EstateCompanyController {

	@Autowired
	private EstateCompanyService estateCompanyService;
	@Autowired
	private CompanyBuildingService companyBuildingService;
	@Autowired
	private IUserService userService;

	/**
	 * 新增 物业公司
	 */
	@PostMapping()
	public R<Boolean> save(@Valid @RequestBody EstateCompanyDTO estateCompany) {
		estateCompanyService.save(estateCompany);

		if (CollUtil.isEmpty(estateCompany.getBuildingIdList())) {
			throw new ServiceException("物业公司必须绑定至少一个小区");
		}
		List<CompanyBuilding> companyBuildingList = new ArrayList<>();
		for (Long buildingId : estateCompany.getBuildingIdList()) {
			CompanyBuilding companyBuilding = new CompanyBuilding();
			companyBuilding.setCompanyId(estateCompany.getId());
			companyBuilding.setBuildingId(buildingId);

			companyBuildingList.add(companyBuilding);
		}

		return R.status(companyBuildingService.saveBatch(companyBuildingList));
	}

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	public R<EstateCompany> detail(@PathVariable Long id) {
		EstateCompany detail = estateCompanyService.getById(id);
		List<Long> companyBuildingList = companyBuildingService.listBuildingIdByCompany(id);
		EstateCompanyDTO estateCompanyDTO = new EstateCompanyDTO();
		BeanUtils.copyProperties(detail,estateCompanyDTO);
		estateCompanyDTO.setBuildingIdList(companyBuildingList);
		return R.data(estateCompanyDTO);
	}

	/**
	 * 分页
	 */
	@GetMapping("")
	public R<IPage<EstateCompany>> list(EstateCompany estateCompany, Query query) {
		IPage<EstateCompany> pages = estateCompanyService.page(Condition.getPage(query), Condition.getQueryWrapper(estateCompany));
		return R.data(pages);
	}

	/**
	 * 修改
	 */
	@PutMapping("/{id}")
	public R<Boolean> update(@PathVariable Long id, @Valid @RequestBody EstateCompanyDTO estateCompany) {
		estateCompany.setId(id);
		companyBuildingService.remove(Wrappers.<CompanyBuilding>lambdaQuery().eq(CompanyBuilding::getCompanyId,estateCompany.getId()));
		List<CompanyBuilding> companyBuildingList = new ArrayList<>();
		for (Long buildingId : estateCompany.getBuildingIdList()) {
			CompanyBuilding companyBuilding = new CompanyBuilding();
			companyBuilding.setCompanyId(estateCompany.getId());
			companyBuilding.setBuildingId(buildingId);

			companyBuildingList.add(companyBuilding);
		}
		companyBuildingService.saveBatch(companyBuildingList);
		return R.status(estateCompanyService.updateById(estateCompany));
	}

	/**
	 * 删除
	 */
	@DeleteMapping()
	public R<Boolean> remove(@RequestParam String ids) {
		return R.status(estateCompanyService.removeById(Func.toLong(ids)));
	}

//	/**
//	 * 获取对应维保公司的人员page
//	 * @param id
//	 * @param roleId
//	 * @param query
//	 * @return
//	 */
//	@GetMapping("/{id}/role/{roleId}")
//	public R<IPage<User>> userPage(@PathVariable Long id, @PathVariable Long roleId, Query query) {
//		IPage<User> pages = userService.selectPageByCompanyId(id, roleId, Condition.getPage(query));
//		return R.data(pages);
//	}

//	/**
//	 * 公司所有维保人员列表
//	 * @return
//	 */
//	@GetMapping("/list")
//	public R<List<MaintainCompanyVO>> list() {
//		List<MaintainCompanyVO> maintainCompanyVOList = maintainCompanyService.maintainCompanyVOList();
//		return R.data(maintainCompanyVOList);
//	}

}
